<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>QT 日志框架系统 | Vico的个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="在最近做的项目上想要弄一个日志管理系统。 最先，自己规划做一个日志系统，单纯的由控制器发送报警信息，或者运行信息，传到上位机，在上位机上面进行文件读写工作。后来在开会时，将这个想法给老大一说，就变成了，在上位机上进行鼠标操作，或者文件参数保存，修改等等，也都要在日志里面显示，可以供客户查看，我一想这太麻烦了，每个要保存在日志的地方都要使用文件读功能，后面讨论时得出还有，防止文件过大导致读写效率慢，">
<meta property="og:type" content="article">
<meta property="og:title" content="QT 日志框架系统">
<meta property="og:url" content="https://vicolj.gitee.io/2021/03/08/QT%20%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6%E7%B3%BB%E7%BB%9F/index.html">
<meta property="og:site_name" content="Vico的个人博客">
<meta property="og:description" content="在最近做的项目上想要弄一个日志管理系统。 最先，自己规划做一个日志系统，单纯的由控制器发送报警信息，或者运行信息，传到上位机，在上位机上面进行文件读写工作。后来在开会时，将这个想法给老大一说，就变成了，在上位机上进行鼠标操作，或者文件参数保存，修改等等，也都要在日志里面显示，可以供客户查看，我一想这太麻烦了，每个要保存在日志的地方都要使用文件读功能，后面讨论时得出还有，防止文件过大导致读写效率慢，">
<meta property="og:locale">
<meta property="article:published_time" content="2021-03-08T13:50:34.000Z">
<meta property="article:modified_time" content="2021-03-09T14:51:37.792Z">
<meta property="article:author" content="Vico">
<meta property="article:tag" content="QT">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Vico的个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Vico的个人博客</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">Vico的个人博客</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://vicolj.gitee.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-QT 日志框架系统" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/03/08/QT%20%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6%E7%B3%BB%E7%BB%9F/" class="article-date">
  <time class="dt-published" datetime="2021-03-08T13:50:34.000Z" itemprop="datePublished">2021-03-08</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      QT 日志框架系统
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>在最近做的项目上想要弄一个日志管理系统。</p>
<p>最先，自己规划做一个日志系统，单纯的由控制器发送报警信息，或者运行信息，传到上位机，在上位机上面进行文件读写工作。后来在开会时，将这个想法给老大一说，就变成了，在上位机上进行鼠标操作，或者文件参数保存，修改等等，也都要在日志里面显示，可以供客户查看，我一想这太麻烦了，每个要保存在日志的地方都要使用文件读功能，后面讨论时得出还有，防止文件过大导致读写效率慢，就要多个文件一起存储，还得定期删除。</p>
<p>后来在网上搜索得知，由开源的日志框架系统，例如log4j转化过来的 log4puls 、log4c 以及 log4qt ，想着，要是找不到更好方法的就用log4qt了，虽然文件多一点，编译成库也就可以了，最后我看到一篇博客，专门介绍了一下用qt自带 qInstallMessageHandler () 函数来做，有很多值得学习的地方，例如QMuterlock锁的使用方法，以及单例模式的使用，还有全局的参数的妙用，我在他的基础上也添加了一些功能，比如说：当文件个数达到30个时删除30个之前的文件（这里使用的是每天创建一个文件）</p>
<p>上代码：</p>
<h2 id="LogHandler-h"><a href="#LogHandler-h" class="headerlink" title="LogHandler.h"></a>LogHandler.h</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#ifndef LOGHANDLER_H</span><br><span class="line">#define LOGHANDLER_H</span><br><span class="line">#include &quot;Singleton.h&quot;</span><br><span class="line"></span><br><span class="line">#define LogHandlerInstance Singleton&lt;LogHandler&gt;::getInstance()</span><br><span class="line">struct LogHandlerPrivate;</span><br><span class="line">class LogHandler &#123;    </span><br><span class="line">    SINGLETON(LogHandler) &#x2F;&#x2F; 使用单例模式public:    </span><br><span class="line">    void uninstallMessageHandler(); &#x2F;&#x2F; 释放资源    </span><br><span class="line">    void installMessageHandler();   &#x2F;&#x2F; 给 Qt 安装消息处理函数</span><br><span class="line">    private:    </span><br><span class="line">    LogHandlerPrivate *d;</span><br><span class="line">&#125;;</span><br><span class="line">#endif &#x2F;&#x2F; LOGHANDLER_H</span><br></pre></td></tr></table></figure>

<h2 id="LogHandler-cpp"><a href="#LogHandler-cpp" class="headerlink" title="LogHandler.cpp"></a>LogHandler.cpp</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#96;#include &quot;LogHandler.h&quot;#include #include #include #include #include #include #include #include #include #include #include #include #include &#x2F;************************************************************************************************************ *                                                                                                          * *                                               LogHandlerPrivate                                          * *                                                                                                          * ***********************************************************************************************************&#x2F;struct LogHandlerPrivate &#123;    LogHandlerPrivate();    ~LogHandlerPrivate();    &#x2F;&#x2F; 打开日志文件 log.txt，如果日志文件不是当天创建的，则使用创建日期把其重命名为 yyyy-MM-dd.log，并重新创建一个 log.txt    void openAndBackupLogFile();    &#x2F;&#x2F; 消息处理函数    static void messageHandler(QtMsgType type, const QMessageLogContext &amp;context, const QString &amp;msg);    &#x2F;&#x2F; 如果日志所在目录不存在，则创建    void makeSureLogDirectory() const;    QDir   logDir;              &#x2F;&#x2F; 日志文件夹    QTimer renameLogFileTimer;  &#x2F;&#x2F; 重命名日志文件使用的定时器    QTimer flushLogFileTimer;   &#x2F;&#x2F; 刷新输出到日志文件的定时器    QDate  logFileCreatedDate;  &#x2F;&#x2F; 日志文件创建的时间    static QFile *logFile;      &#x2F;&#x2F; 日志文件    static QTextStream *logOut; &#x2F;&#x2F; 输出日志的 QTextStream，使用静态对象就是为了减少函数调用的开销    static QMutex logMutex;     &#x2F;&#x2F; 同步使用的 mutex&#125;;&#x2F;&#x2F; 初始化 static 变量QMutex LogHandlerPrivate::logMutex;QFile* LogHandlerPrivate::logFile &#x3D; nullptr;QTextStream* LogHandlerPrivate::logOut &#x3D; nullptr;LogHandlerPrivate::LogHandlerPrivate() &#123;    logDir.setPath(&quot;log&quot;); &#x2F;&#x2F; TODO: 日志文件夹的路径，为 exe 所在目录下的 log 文件夹，可从配置文件读取    QString logPath &#x3D; logDir.absoluteFilePath(&quot;log.txt&quot;); &#x2F;&#x2F; 日志的路径    &#x2F;&#x2F; 日志文件创建的时间    &#x2F;&#x2F; QFileInfo::created(): On most Unix systems, this function returns the time of the last status change.    &#x2F;&#x2F; 所以不能运行时使用这个函数检查创建时间，因为会在运行时变化，于是在程序启动时保存下日志文件的最后修改时间，    &#x2F;&#x2F; 在后面判断如果不是今天则用于重命名 log.txt    &#x2F;&#x2F; 如果是 Qt 5.10 后，lastModified() 可以使用 birthTime() 代替    logFileCreatedDate &#x3D; QFileInfo(logPath).lastModified().date();    &#x2F;&#x2F; 打开日志文件，如果不是当天创建的，备份已有日志文件    openAndBackupLogFile();    &#x2F;&#x2F; 十分钟检查一次日志文件创建时间    renameLogFileTimer.setInterval(1000 * 60 * 10); &#x2F;&#x2F; TODO: 可从配置文件读取    &#x2F;&#x2F; renameLogFileTimer.setInterval(1000); &#x2F;&#x2F; 为了快速测试看到日期变化后是否新创建了对应的日志文件，所以 1 秒检查一次    renameLogFileTimer.start();    QObject::connect(&amp;renameLogFileTimer, &amp;QTimer::timeout, [this] &#123;        QMutexLocker locker(&amp;LogHandlerPrivate::logMutex);        openAndBackupLogFile();    &#125;);    &#x2F;&#x2F; 定时刷新日志输出到文件，尽快的能在日志文件里看到最新的日志    flushLogFileTimer.setInterval(1000); &#x2F;&#x2F; TODO: 可从配置文件读取    flushLogFileTimer.start();    QObject::connect(&amp;flushLogFileTimer, &amp;QTimer::timeout, [] &#123;        &#x2F;&#x2F; qDebug() &lt;&lt; QDateTime::currentDateTime().toString(&quot;yyyy-MM-dd hh:mm:ss&quot;); &#x2F;&#x2F; 测试不停的写入内容到日志文件        QMutexLocker locker(&amp;LogHandlerPrivate::logMutex);        if (nullptr !&#x3D; logOut) &#123;            logOut-&gt;flush();        &#125;    &#125;);&#125;LogHandlerPrivate::~LogHandlerPrivate() &#123;    if (nullptr !&#x3D; logFile) &#123;        logFile-&gt;flush();        logFile-&gt;close();        delete logOut;        delete logFile;        &#x2F;&#x2F; 因为他们是 static 变量        logOut  &#x3D; nullptr;        logFile &#x3D; nullptr;    &#125;&#125;&#x2F;&#x2F; 打开日志文件 log.txt，如果不是当天创建的，则使用创建日期把其重命名为 yyyy-MM-dd.log，并重新创建一个 log.txtvoid LogHandlerPrivate::openAndBackupLogFile() &#123;    &#x2F;&#x2F; 总体逻辑:    &#x2F;&#x2F; 1. 程序启动时 logFile 为 nullptr，初始化 logFile，有可能是同一天打开已经存在的 logFile，所以使用 Append 模式    &#x2F;&#x2F; 2. logFileCreatedDate is nullptr, 说明日志文件在程序开始时不存在，所以记录下创建时间    &#x2F;&#x2F; 3. 程序运行时检查如果 logFile 的创建日期和当前日期不相等，则使用它的创建日期重命名，然后再生成一个新的 log.txt 文件    makeSureLogDirectory(); &#x2F;&#x2F; 如果日志所在目录不存在，则创建    QString logPath &#x3D; logDir.absoluteFilePath(&quot;log.txt&quot;); &#x2F;&#x2F; 日志的路径    &#x2F;&#x2F; [[1]] 程序启动时 logFile 为 nullptr    if (nullptr &#x3D;&#x3D; logFile) &#123;        logFile &#x3D; new QFile(logPath);        logOut  &#x3D; (logFile-&gt;open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) ?  new QTextStream(logFile) : nullptr;        if (nullptr !&#x3D; logOut) &#123;            logOut-&gt;setCodec(&quot;UTF-8&quot;);        &#125;        &#x2F;&#x2F; [[2]] 如果文件是第一次创建，则创建日期是无效的，把其设置为当前日期        if (logFileCreatedDate.isNull()) &#123;            logFileCreatedDate &#x3D; QDate::currentDate();        &#125;        &#x2F;&#x2F; TODO: 可以检查日志文件超过 30 个，删除 30 天前的日志文件    &#125;    &#x2F;&#x2F; [[3]] 程序运行时如果创建日期不是当前日期，则使用创建日期重命名，并生成一个新的 log.txt    if (logFileCreatedDate !&#x3D; QDate::currentDate()) &#123;        logFile-&gt;flush();        logFile-&gt;close();        delete logOut;        delete logFile;        QString newLogPath &#x3D; logDir.absoluteFilePath(logFileCreatedDate.toString(&quot;yyyy-MM-dd.log&quot;));;        QFile::copy(logPath, newLogPath); &#x2F;&#x2F; Bug: 按理说 rename 会更合适，但是 rename 时最后一个文件总是显示不出来，需要 killall Finder 后才出现        QFile::remove(logPath); &#x2F;&#x2F; 删除重新创建，改变创建时间        logFile &#x3D; new QFile(logPath);        logOut  &#x3D; (logFile-&gt;open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) ?  new QTextStream(logFile) : nullptr;        logFileCreatedDate &#x3D; QDate::currentDate();        if (nullptr !&#x3D; logOut) &#123;            logOut-&gt;setCodec(&quot;UTF-8&quot;);        &#125;    &#125;&#125;&#x2F;&#x2F; 如果日志所在目录不存在，则创建void LogHandlerPrivate::makeSureLogDirectory() const &#123;    if (!logDir.exists()) &#123;        logDir.mkpath(&quot;.&quot;); &#x2F;&#x2F; 可以递归的创建文件夹    &#125;&#125;&#x2F;&#x2F; 消息处理函数void LogHandlerPrivate::messageHandler(QtMsgType type, const QMessageLogContext &amp;context, const QString &amp;msg) &#123;    QMutexLocker locker(&amp;LogHandlerPrivate::logMutex);    QString level;    switch (type) &#123;    case QtDebugMsg:        level &#x3D; &quot;DEBUG&quot;;        break;    case QtInfoMsg:        level &#x3D; &quot;INFO &quot;;        break;    case QtWarningMsg:        level &#x3D; &quot;WARN &quot;;        break;    case QtCriticalMsg:        level &#x3D; &quot;ERROR&quot;;        break;    case QtFatalMsg:        level &#x3D; &quot;FATAL&quot;;        break;    default:        break;    &#125;    &#x2F;&#x2F; 输出到标准输出: Windows 下 std::cout 使用 GB2312，而 msg 使用 UTF-8，但是程序的 Local 也还是使用 UTF-8#if defined(Q_OS_WIN)    QByteArray localMsg &#x3D; QTextCodec::codecForName(&quot;GB2312&quot;)-&gt;fromUnicode(msg); &#x2F;&#x2F;msg.toLocal8Bit();#else    QByteArray localMsg &#x3D; msg.toLocal8Bit();#endif    std::cout &lt;&lt; std::string(localMsg) &lt;&lt; std::endl;    if (nullptr &#x3D;&#x3D; LogHandlerPrivate::logOut) &#123;        return;    &#125;    &#x2F;&#x2F; 输出到日志文件, 格式: 时间 - [Level] (文件名:行数, 函数): 消息    QString fileName &#x3D; context.file;    int index &#x3D; fileName.lastIndexOf(QDir::separator());    fileName &#x3D; fileName.mid(index + 1);    (*LogHandlerPrivate::logOut) &lt;&lt; QString(&quot;%1 - [%2] (%3:%4, %5): %6\n&quot;)                                    .arg(QDateTime::currentDateTime().toString(&quot;yyyy-MM-dd hh:mm:ss&quot;)).arg(level)                                    .arg(fileName).arg(context.line).arg(context.function).arg(msg);&#125;&#x2F;************************************************************************************************************ *                                                                                                          * *                                               LogHandler                                                 * *                                                                                                          * ***********************************************************************************************************&#x2F;LogHandler::LogHandler() : d(nullptr) &#123;&#125;LogHandler::~LogHandler() &#123;&#125;void LogHandler::installMessageHandler() &#123;    QMutexLocker locker(&amp;LogHandlerPrivate::logMutex);    if (nullptr &#x3D;&#x3D; d) &#123;        d &#x3D; new LogHandlerPrivate();        qInstallMessageHandler(LogHandlerPrivate::messageHandler); &#x2F;&#x2F; 给 Qt 安装自定义消息处理函数    &#125;&#125;void LogHandler::uninstallMessageHandler() &#123;    QMutexLocker locker(&amp;LogHandlerPrivate::logMutex);    qInstallMessageHandler(nullptr);    delete d;    d &#x3D; nullptr;&#125;&#96;</span><br></pre></td></tr></table></figure>

<h2 id="Singleton-h"><a href="#Singleton-h" class="headerlink" title="Singleton.h"></a>Singleton.h</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line">#ifndef SINGLETON_H</span><br><span class="line">#define SINGLETON_H</span><br><span class="line"></span><br><span class="line">#include &lt;QMutex&gt;</span><br><span class="line">#include &lt;QScopedPointer&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                                                                          &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                            Singleton signature                           &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                                                                          &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;**</span><br><span class="line"></span><br><span class="line"> * 使用方法:</span><br><span class="line"></span><br><span class="line"> * 1. 定义类为单例:</span><br><span class="line"></span><br><span class="line"> * class ConnectionPool &#123;</span><br><span class="line"></span><br><span class="line"> * SINGLETON(ConnectionPool) &#x2F;&#x2F; Here</span><br><span class="line"></span><br><span class="line"> * public:</span><br><span class="line">    *</span><br><span class="line"></span><br><span class="line"> * 2. 实现无参构造函数，析构函数</span><br><span class="line"></span><br><span class="line"> * 3. 获取单例类的对象:</span><br><span class="line"></span><br><span class="line"> * Singleton&lt;ConnectionPool&gt;::getInstance();</span><br><span class="line"></span><br><span class="line"> * ConnectionPool &amp;pool &#x3D; Singleton&lt;ConnectionPool&gt;::getInstance();</span><br><span class="line"></span><br><span class="line"> * 注意: 如果单例的类需要释放的资源和 Qt 底层的信号系统有关系，例如 QSettings，QSqlDatabase 等，</span><br><span class="line"></span><br><span class="line"> * 需要在程序结束前手动释放(也就是在 main() 函数返回前调用释放资源的函数)，</span><br><span class="line"></span><br><span class="line"> * 否则有可能在程序退出时报系统底层的信号错误，导致如 QSettings 的数据没有保存。</span><br><span class="line">    *&#x2F;</span><br><span class="line">   template &lt;typename T&gt;</span><br><span class="line">   class Singleton &#123;</span><br><span class="line">   public:</span><br><span class="line">   static T&amp; getInstance();</span><br><span class="line"></span><br><span class="line">   Singleton(const Singleton &amp;other);</span><br><span class="line">   Singleton&lt;T&gt;&amp; operator&#x3D;(const Singleton &amp;other);</span><br><span class="line"></span><br><span class="line">private:</span><br><span class="line">    static QMutex mutex;</span><br><span class="line">    static QScopedPointer&lt;T&gt; instance;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                                                                          &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                            Singleton definition                          &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                                                                          &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;</span><br><span class="line">template &lt;typename T&gt; QMutex Singleton&lt;T&gt;::mutex;</span><br><span class="line">template &lt;typename T&gt; QScopedPointer&lt;T&gt; Singleton&lt;T&gt;::instance;</span><br><span class="line"></span><br><span class="line">template &lt;typename T&gt;</span><br><span class="line">T&amp; Singleton&lt;T&gt;::getInstance() &#123;</span><br><span class="line">    if (instance.isNull()) &#123;</span><br><span class="line">        mutex.lock();</span><br><span class="line">        if (instance.isNull()) &#123;</span><br><span class="line">            instance.reset(new T());</span><br><span class="line">        &#125;</span><br><span class="line">        mutex.unlock();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return *instance.data();</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                                                                          &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                               Singleton Macro                            &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;                                                                          &#x2F;&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;&#x2F;</span><br><span class="line">#define SINGLETON(Class)                        \</span><br><span class="line">private:                                        \</span><br><span class="line">    Class();                                    \</span><br><span class="line">    ~Class();                                   \</span><br><span class="line">    Class(const Class &amp;other);                  \</span><br><span class="line">    Class&amp; operator&#x3D;(const Class &amp;other);       \</span><br><span class="line">    friend class Singleton&lt;Class&gt;;              \</span><br><span class="line">    friend struct QScopedPointerDeleter&lt;Class&gt;;</span><br><span class="line"></span><br><span class="line">#endif &#x2F;&#x2F; SINGLETON_H</span><br></pre></td></tr></table></figure>





<h2 id="思考"><a href="#思考" class="headerlink" title="思考"></a>思考</h2><ol>
<li>main() 函数里的 qDebug() 输出都是在 UI 线程，LogHandler 是否多线程安全？怎么测试？</li>
<li>日志的相关配置数据例如输出目录等都是写死在程序里的，如果写到配置文件里是不是更灵活？</li>
<li>日志的格式也是写死在程序里的，如果能做到通过配置修改日志格式那就更强大了，就像 <code>log4cxx</code> 一样</li>
<li>测试如何快速的看到不同日期生成的日志文件不同？</li>
<li>删除超过 30 天的日志</li>
<li>单个日志文件例如大于 100M 后重新创建一个新的日志文件</li>
</ol>
<p>接下来的思考部分，我会慢慢都加在代码当中，也希望大家可以一起学习一下，最后特别鸣谢</p>
<p><a target="_blank" rel="noopener" href="http://qtdebug.com/all/">公孙二狗</a></p>
<p>文章参考链接：<a target="_blank" rel="noopener" href="http://qtdebug.com/qt-logger/">http://qtdebug.com/qt-logger/</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://vicolj.gitee.io/2021/03/08/QT%20%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6%E7%B3%BB%E7%BB%9F/" data-id="ckm0o9s8w0000jod5g5f740uv" data-title="QT 日志框架系统" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/QT/" rel="tag">QT</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/03/13/const%20%E4%B8%8E%20constexpr%20%E7%9A%84%E5%8C%BA%E5%88%AB/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/2021/03/08/QT-%E7%BF%BB%E8%AF%91-%E6%9C%89%E5%85%B3tr%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">QT 翻译 有关tr的那些事</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/IEC%E8%A7%84%E8%8C%83/" rel="tag">IEC规范</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/QT/" rel="tag">QT</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/IEC%E8%A7%84%E8%8C%83/" style="font-size: 10px;">IEC规范</a> <a href="/tags/QT/" style="font-size: 20px;">QT</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">March 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/03/29/IEC104%E8%A7%84%E8%8C%83/">IEC104规范</a>
          </li>
        
          <li>
            <a href="/2021/03/13/const%20%E4%B8%8E%20constexpr%20%E7%9A%84%E5%8C%BA%E5%88%AB/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/03/08/QT%20%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6%E7%B3%BB%E7%BB%9F/">QT 日志框架系统</a>
          </li>
        
          <li>
            <a href="/2021/03/08/QT-%E7%BF%BB%E8%AF%91-%E6%9C%89%E5%85%B3tr%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B/">QT 翻译 有关tr的那些事</a>
          </li>
        
          <li>
            <a href="/2021/03/08/%E4%BD%BF%E7%94%A8%20QVariant%20%E4%BC%A0%E9%80%92%E6%8C%87%E9%92%88%E7%9A%84%E5%A6%99%E7%94%A8/">使用 QVariant 传递指针   的妙用</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Vico<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>